package cn.htu.edu.utils;

import cn.hutool.core.lang.Snowflake;

import java.text.SimpleDateFormat;
import java.time.Instant;

/**
 * Copyright (C), 2024-04-30
 * FileName: TimestampGenerator
 * Author:   dzk
 * Date:     2024/4/30 16:36
 * Description: 18位id生成
 */

public class TimestampGenerator {
    public static String timestampMillis() {
        // 获取当前时间的Instant对象，Instant类提供了访问纳秒级时间的支持
        Instant now = Instant.now();
        // 转换为毫秒级别的时间戳
        long timestampMillis = now.toEpochMilli();
        // 使用SimpleDateFormat格式化为18位的时间戳字符串，格式为yyyyMMddHHmmssSSSS
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddMHHmmssSSSS");
        return sdf.format(timestampMillis);
    }

    public static Long snowflakeMillis() {
        Snowflake companyId = cn.hutool.core.util.IdUtil.createSnowflake(3, 1);
        return companyId.nextId();
    }

    public static void main(String[] args) {
        System.out.println(TimestampGenerator.timestampMillis());
        System.out.println(TimestampGenerator.snowflakeMillis());
    }
}
